import { GCtrl } from "../../lib/util/GCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BindShangJi extends cc.Component {

    @property(cc.EditBox)
    phoneEdit: cc.EditBox = null;
    @property(cc.EditBox)
    vfCodeEdit: cc.EditBox = null;
    private myInfo = GCtrl.session.getPlayer();

    onEnable() {
        this.onShow();
    }
    onDisable() {
        this.onHide();
    }


    private checkPhone() {
        let str = this.phoneEdit.string;
        if (!str) {
            GCtrl.GLUtils.showTips("请输入上级代理号");
            return false;
        }
        return true;
    }

    private sendVerifyCode() {
        let cmd = "GetVcode";
        let msg: any = {
            phone_number: this.phoneEdit.string
        }
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcode)
                GCtrl.GLUtils.showTips(data.errcodedes);
            else {
                GCtrl.GLUtils.showTips("发送成功");
            }
        });
    }

    private bindPhoneReq() {
        cc.vv.GLUtils.showAlert1("邀请码绑定不可逆, 确认操作?", () => {
            cc.vv.GLUtils.sendRequest('RegisterAgent', { promocode: this.phoneEdit.string }, (err, data) => {
                if (data.errcodedes) {
                    cc.vv.GLUtils.showTips(data.errcodedes);
                }
                else {
                    cc.vv.GLUtils.showTips('绑定成功');
                    cc.vv.session.setParentRid(data.parent_rid);
                    cc.vv.eventCustom.emit("bindAgentSucess")
                    cc.vv.UIMgr.close(cc.vv.GLConfig.FPJ_BindShangJi);

                    //打开个人信息 change by tm
                    GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_PersonalInfo_path, null, function () {
                        //打开修改头像
                        GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_ChangeHead_path);
                    }.bind(this));

                }
            });
        }, function () { });
    }
    onClickBack() {
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_BindPhone_path, GCtrl.UIMgr.closeType.hide);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickSendVerifyCode() {
        this.sendVerifyCode();
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickConfirm() {
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        if (!this.checkPhone()) return;
        this.bindPhoneReq();
    }
    onShow() {
        this.phoneEdit.string = this.getRightClipboard();
    }
    onHide() {
        this.phoneEdit.string = "";
        this.vfCodeEdit.string = "";
    }


    getRightClipboard() {
        // let str = cc.vv.GLSDKMgr.getClipboard() || "";
        // let numStr = "";
        //
        // //去掉非数字字符
        // for(let i in str){
        //     let c = str[i];
        //     if(c >= '0' && c<= '9'){
        //         numStr += c;
        //     }
        // }
        //
        // if( numStr.length == 7){        //邀请码七位数
        //     return numStr;
        // }else {
        //     return  "";
        // }

        try {
            let str = cc.vv.GLSDKMgr.getClipboard() || "";
            let ob = JSON.parse(str);
            if (ob && ob.rid) {
                return ob.rid;
            } else {
                return "";
            }
        } catch (e) {
            return "";
        }

    }
}
